package de.koller.aframe.plain.gui;

import de.koller.aframe.plain.ADisplayPlain;
import de.koller.aframe.plain.IARootPlain;
import de.koller.aframe.util.node.NodeA;
import de.koller.aframe.plain.gui.component.Component;
import de.koller.aframe.plain.gui.input.GUIInput;
import de.koller.aframe.plain.gui.input.IInputHandler;
import de.koller.aframe.plain.gui.input.SyncInputHandler;
import de.koller.oceanFramework.OceanException;
import java.awt.Graphics2D;

public class GUIRoot extends NodeA implements IARootPlain {
	
	public final ADisplayPlain display;
	private GUIInput input = null;
	private final IGUIControlRoot controlRoot;
	public final Component baseComponent;
	
	public GUIRoot( int width, int height, String title, IGUIControlRoot controlRoot ) {
		display = new ADisplayPlain( this, width, height, title );
		this.controlRoot = controlRoot;
	
		baseComponent = new Component();
		baseComponent.bounds.set( 0, 0, width, height );
	}

	@Override
	protected void init() {
		super.init();
		
		display.init();
		display.clearScreen = false;
	}

	@Override
	protected void release() {
		super.release();
		
		display.release();
	}
	
	public void updateDisplay() {
		display.render();
	}
	
	@Override
	public void displayClosed() {
		controlRoot.stopRoot();
	}

	@Override
	public void render( Graphics2D g ) {
		baseComponent.renderThis( g );
	}
	
	@Override
	public long getSystemTime() {
		return System.currentTimeMillis();
	}
	
	public void initInput( IInputHandler inputHandler ) {
		if( input != null )
			throw new OceanException( "input already set!" );
		
		input = new GUIInput( display, new SyncInputHandler( controlRoot, inputHandler ) );
		this.addChild( input );
	}
	
}
